package com.lhgray.sglhome.presentation.viewModel

import android.net.Uri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.freightoa.foundation.viewmodel.BaseState
import com.freightoa.foundation.viewmodel.UiIntent
import com.lhgray.sglhome.domain.model.SourceData
import com.lhgray.sglhome.domain.usecase.WarehousePostUseCase
import com.lhgray.sglhome.domain.usecase.WarehouseUseCase
import com.lhgray.sglhome.domain.model.WarehouseBean
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * 仓储的页面
 */
sealed class WareState {
    data class Loading(val isLoading: Boolean) : WareState()
    data class Error(val errorMessage: Int) : WareState()
    data class Success(val data: WarehouseState) : WareState()
    data class Toast(val toast: String?) : WareState()
    data object Ide : WareState()
}

data class WarehouseState(
    val warehouseBean: WarehouseBean? = null,
    val isLoading: Boolean = false,
    val errorMsg: String? = null,
    val toast: String? = null,

    ) : BaseState


@HiltViewModel
class WarehouseViewModel @Inject constructor(
    private val warehouseUseCase: WarehouseUseCase,
    private val warehousePostUseCase: WarehousePostUseCase,
) : ViewModel() {
    private val _wareState: MutableStateFlow<WareState> = MutableStateFlow(WareState.Ide)
    private val _warehouseBean: MutableStateFlow<WarehouseBean> = MutableStateFlow(WarehouseBean())
    private val _loading: MutableStateFlow<Boolean> = MutableStateFlow(false)
    private val _errorMsg: MutableStateFlow<Throwable?> = MutableStateFlow(null)

    val uiState: StateFlow<WarehouseState> = combine(
        _warehouseBean, _loading, _errorMsg, _wareState
    ) { warehouseBean, loading, errorMsg, wareState ->
        when (wareState) {
            is WareState.Error -> {
                WarehouseState(warehouseBean, false, errorMsg?.message)
            }

            is WareState.Loading -> {
                WarehouseState(warehouseBean, true, null)
            }

            is WareState.Success -> {
                WarehouseState(warehouseBean, false, null)
            }

            WareState.Ide -> WarehouseState()
            is WareState.Toast -> WarehouseState(warehouseBean, false, null, wareState.toast)
        }

    }.stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(5000),
        initialValue = WarehouseState(WarehouseBean(), isLoading = false)
    )


    fun getWarehouseJob(fileNo: String) {
        if (fileNo.isEmpty()) return
        _wareState.value = WareState.Loading(true)
        viewModelScope.launch {
            val result = warehouseUseCase.invoke(fileNo)
            if (result.isSuccess) {
                val orDefault = result.getOrDefault(WarehouseBean())!!.copy()
                _warehouseBean.value = orDefault
                _wareState.value = WareState.Success(WarehouseState())
            } else {
                val message = result.exceptionOrNull()
                _errorMsg.value = message
                _wareState.value = WareState.Error(0)
            }
        }
    }

    fun postWarehouseJob(copy: WarehouseBean) {
        _wareState.value = WareState.Loading(true)
        viewModelScope.launch {
            val result = warehousePostUseCase.invoke(copy)
            if (result.isSuccess) {
                val orNull = result.getOrNull()
                _wareState.value = WareState.Toast(orNull?.message_en)
            } else {
                val message = result.exceptionOrNull()
                _errorMsg.value = message
                _wareState.value = WareState.Error(0)
            }
        }
    }


    fun uploadwarehouseBean(copy: WarehouseBean) {
        _warehouseBean.value = copy.copy()
    }
}

// 登录意图
sealed interface WarehouseIntent : UiIntent {
    data class Warehouse(val list: MutableList<Uri>) : WarehouseIntent
}